﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using Infrastructure;
using SverigesRadio.SverigesRadioApp.Model;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels
{
    public class ProgramInformationViewModel : ViewModelBase
    {
        public const string ProgramIdQueryString = "ProgramId";
        public const string ProgramTitleQueryString = "ProgramTitle";

        private SverigesRadioServiceCaller<RssFeed<ProgramInformation>> programService;

        public ProgramInformationViewModel(MainViewModel parentViewModel)
        {
            ParentViewModel = parentViewModel;
            ParentViewModel.NavigationService.Navigated += NavigationService_Navigated;

            programService = new SverigesRadioServiceCaller<RssFeed<ProgramInformation>>();
            programService.OnServiceCallReceived += ProgramService_OnServiceCallReceived;
        }

        private void ProgramService_OnServiceCallReceived(object sender, SverigesRadioServiceCallerEventArgs<RssFeed<ProgramInformation>> e)
        {
            ParentViewModel.IsBusy = false;


        }


        public MainViewModel ParentViewModel { get; private set; }

        private string categoryTitleText;

        public string CategoryTitleText
        {
            get { return categoryTitleText; }
            set
            {
                categoryTitleText = value;

                RaisePropertyChanged("CategoryTitleText");
            }
        }

        private string programTitle;
        public string ProgramTitle
        {
            get { return programTitle; }
            set
            {
                programTitle = value;
                RaisePropertyChanged("ProgramTitle");
            }
        }


        private void NavigationService_Navigated(object sender, NavigatedEventHandler e)
        {
            if (e.ViewType != ViewType.ProgramInformation)
                return;

            string programId = e.QueryStrings[ProgramIdQueryString];
            
            CategoryTitleText = e.QueryStrings[ProgramsViewModel.CategoryIdQueryString];
            ProgramTitle = e.QueryStrings[ProgramTitleQueryString];


            ParentViewModel.IsBusy = true;
            programService.CallAsync(SverigesRadioServices.ProgramInformation, string.Format("rssfeed={0}", programId));


        }


    }
}
